package net.fenghaitao;

import lombok.extern.slf4j.Slf4j;
import net.fenghaitao.domain.ExcelSetting;
import net.fenghaitao.handler.AutoExcelValidHandler;
import net.fenghaitao.parameters.*;
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.util.*;
import java.util.function.Consumer;


@Slf4j
public class AutoExcel {

    /**
     * Generate Excel according to the specified template and parameters
     *
     * @param templatePath       the excel template path
     * @param outputPath         output path
     * @param templateExportPara the parameter for exporting with template
     */
    public static void save(String templatePath, String outputPath, TemplateExportPara templateExportPara) throws Exception {
        save(templatePath, outputPath, Arrays.asList(templateExportPara));
    }

    /**
     * Generate Excel according to the specified template and parameters
     *
     * @param templatePath        the excel template path
     * @param outputPath          output path
     * @param templateExportParas the parameters for exporting with template
     */
    public static void save(String templatePath, String outputPath, List<TemplateExportPara> templateExportParas) throws Exception {
        save(templatePath, outputPath, templateExportParas, null, null);
    }

    /**
     * Generate Excel according to the specified template and parameters
     *
     * @param templatePath        the excel template path
     * @param outputPath          output path
     * @param templateExportParas the parameters for exporting with template
     * @param excelSetting        Excel Setting
     */
    public static void save(String templatePath, String outputPath, List<TemplateExportPara> templateExportParas,
                            ExcelSetting excelSetting) throws Exception {
        ExcelWriter.save(templatePath, outputPath, templateExportParas, excelSetting);
    }

    /**
     * Generate Excel according to the specified template and parameters
     *
     * @param templatePath        the excel template path
     * @param outputPath          output path
     * @param templateExportParas the parameters for exporting with template
     * @param actionAhead         action before internal operations
     * @param actionBehind        action after internal operations
     */
    public static void save(String templatePath,
                            String outputPath,
                            List<TemplateExportPara> templateExportParas,
                            Consumer<Workbook> actionAhead,
                            Consumer<Workbook> actionBehind) throws Exception {
        ExcelWriter.save(templatePath, outputPath, templateExportParas, actionAhead, actionBehind);
    }

    /**
     * Generate Excel directly
     *
     * @param outputPath       output path
     * @param directExportPara the parameter for exporting directly
     */
    public static void save(String outputPath, DirectExportPara directExportPara) throws Exception {
        save(outputPath, Arrays.asList(directExportPara));
    }

    /**
     * Generate Excel directly
     *
     * @param outputPath        output path
     * @param directExportParas the parameters for exporting directly
     */
    public static void save(String outputPath, List<DirectExportPara> directExportParas) throws Exception {
        ExcelWriter.save(outputPath, directExportParas);
    }

//    read

    /**
     * Import data from excel
     *
     * @param fileName    file name to read
     * @param importParas the parameter for import
     */
    public static HashMap<String, List<HashMap<String, Object>>> read(String fileName, List<ImportPara> importParas) throws Exception {
        return ExcelReader.read(fileName, importParas);

    }

    /**
     * Import data from excel
     */
    public static HashMap<String, List<HashMap<String, Object>>> read(FileInputStream fileInputStream, List<ImportPara> importParas) throws Exception {
        return ExcelReader.read(fileInputStream, importParas);
    }

    public static <T> List<T> readByDataSource(String fileName, List<ImportPara> importParas, Class<T> clazz, AutoExcelValidHandler<T> validHandler) throws Exception {
        return ExcelReader.readByDataSource(fileName, importParas, clazz, clazz.getSimpleName(),validHandler);
    }
    public static <T> List<T> readByDataSource(String fileName, Class<T> clazz, AutoExcelValidHandler<T> validHandler) throws Exception {
        return readByDataSource(fileName, Arrays.asList( new ImportPara( clazz.getSimpleName())  ), clazz,validHandler);
    }

    public static <T> List<T> readByDataSource(String fileName, List<ImportPara> importParas, Class<T> clazz, String dataSourceName, AutoExcelValidHandler<T> validHandler) throws Exception {
        return ExcelReader.readByDataSource(fileName, importParas, clazz, dataSourceName,validHandler);
    }


}